package com.supreme.cacheserver.controller;

import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping(value = "redis-zset")
public class RedisZSetController {

    @Resource
    private RedisTemplate<String, Serializable> redisTemplate;

    @Resource
    private List<RedisTemplate<String, Serializable>> redisTemplateList;

    @GetMapping(value = "score/{key}/{val}")
    public AjaxResp score(@PathVariable(value = "key") String key, @PathVariable(value = "val") Object... val) {
        List<Double> score = redisTemplate.opsForZSet().score(key, val);
        return AjaxResp.instance(score);
    }

    @GetMapping(value = "range/{key}")
    public AjaxResp range(@PathVariable(value = "key") String key,
                          @RequestParam(value = "start", defaultValue = "0") long start,
                          @RequestParam(value = "end", defaultValue = "-1") long end) {
        Set<Serializable> members = redisTemplate.opsForZSet().range(key, start, end);
        return AjaxResp.instance(members);
    }

    @GetMapping(value = "reverseRange/{key}")
    public AjaxResp reverseRange(@PathVariable(value = "key") String key,
                          @RequestParam(value = "start", defaultValue = "0") long start,
                          @RequestParam(value = "end", defaultValue = "-1") long end) {
        Set<Serializable> members = redisTemplate.opsForZSet().reverseRange(key, start, end);
        return AjaxResp.instance(members);
    }

    @GetMapping(value = "add/{key}/{val}/{score}")
    public AjaxResp add(@PathVariable(value = "key") String key,
                            @PathVariable(value = "val") String val,
                            @PathVariable(value = "score") long score) {
        redisTemplate.opsForZSet().add(key, val, score);
        return AjaxResp.instance();
    }

    @GetMapping(value = "remove/{key}/{val}")
    public AjaxResp remove(@PathVariable(value = "key") String key,
                            @PathVariable(value = "val") Object... val) {
        Long remove = redisTemplate.opsForZSet().remove(key, val);
        return AjaxResp.instance(remove);
    }

    @GetMapping(value = "rangeByScore/{key}/{min}/{max}")
    public AjaxResp rangeByScore(@PathVariable(value = "key") String key,
                                    @PathVariable(value = "min") double min,
                                    @PathVariable(value = "max") double max) {
        Set<Serializable> members = redisTemplate.opsForZSet().rangeByScore(key, min, max);
        return AjaxResp.instance(members);
    }

    @GetMapping(value = "incrementScore/{key}/{val}/{delta}")
    public AjaxResp incrementScore(@PathVariable(value = "key") String key,
                                    @PathVariable(value = "val") String val,
                                    @PathVariable(value = "delta") double delta) {
        Double incrementScore = redisTemplate.opsForZSet().incrementScore(key, val, delta);
        return AjaxResp.instance(incrementScore);
    }

    @GetMapping(value = "rank/{key}/{val}")
    public AjaxResp rank(@PathVariable(value = "key") String key, @PathVariable(value = "val") Object val) {
        Long rank = redisTemplate.opsForZSet().rank(key, val);
        return AjaxResp.instance(rank);
    }

    @GetMapping(value = "reverseRank/{key}/{val}")
    public AjaxResp reverseRank(@PathVariable(value = "key") String key, @PathVariable(value = "val") Object val) {
        Long rank = redisTemplate.opsForZSet().reverseRank(key, val);
        return AjaxResp.instance(rank);
    }

    @GetMapping(value = "card/{key}")
    public AjaxResp card(@PathVariable(value = "key") String key) {
        Long card = redisTemplate.opsForZSet().zCard(key);
        return AjaxResp.instance(card);
    }

    @GetMapping(value = "count/{key}/{min}/{max}")
    public AjaxResp count(@PathVariable(value = "key") String key,
                          @PathVariable(value = "min") double min,
                          @PathVariable(value = "max") double max) {
        Long count = redisTemplate.opsForZSet().count(key, min, max);
        return AjaxResp.instance(count);
    }
}
